package com.CmJava.tomcat.Server.service.connector;

import com.CmJava.tomcat.Server.Component;
import com.CmJava.tomcat.Server.service.container.Container;
import com.CmJava.tomcat.Server.service.Service;
import com.CmJava.tomcat.Server.life.Lifecycle;
import com.CmJava.tomcat.Server.life.LifecycleState;
import com.CmJava.tomcat.Server.req_resp.HttpServletRequest;
import com.CmJava.tomcat.Server.req_resp.HttpServletResponse;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Connector implements Lifecycle, Component {
    private LifecycleState lifecycleState = LifecycleState.NEW;
    public static HttpServletRequest request;
    public static HttpServletResponse response;
    public static Component container;
    public static ServerSocket serverSocket;

    static {
        try {
            serverSocket = new ServerSocket(8080);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public Connector() {

    }


    @Override
    public void init() {
        container = Service.container;
        lifecycleState = LifecycleState.INITIALIZED;
    }

    @Override
    public void start() {

        while (true) {
            try {
                Socket socket = serverSocket.accept();
                InputStream inputStream = socket.getInputStream();
                OutputStream outputStream = socket.getOutputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

                String method = bufferedReader.readLine();
                if (method == null) {
                    continue;
                }
                String context=method.split(" ")[1];
                if (method.startsWith("GET")) {
                    method = method.substring(0, 3);
                } else {
                    method = method.substring(0, 4);
                }

                String host="";
                while (true) {
                    String msg = bufferedReader.readLine();
                    if (msg.length()==0){
                        break;
                    }
                    if (msg.startsWith("Host")){
                        host=msg.split(":")[1].substring(1);
                        break;
                    }

                }

                request = new HttpServletRequest(method,host,context,inputStream);
                response = new HttpServletResponse(outputStream);


                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Container container = (Container) Connector.container;
                        container.start();
                        try {
                            if (socket != null)
                                socket.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();

            } catch (IOException e) {
                e.printStackTrace();
            } finally {

            }
        }

    }

    @Override
    public void stop() {
        try {
            if (serverSocket != null)
                serverSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void destroy() {
        stop();
    }

    @Override
    public LifecycleState getState() {
        return null;
    }

    @Override
    public Component getChild() {
        return null;
    }

    @Override
    public void setChild(Component child) {

    }
}
